@using BTCPayServer.Services.Stores
@using BTCPayServer.Client
@using Microsoft.AspNetCore.Mvc.TagHelpers
@model StoreUsersViewModel
@inject StoreRepository StoreRepository
@{
    var storeId = Model.StoreId;
    var roles = new SelectList(
        await StoreRepository.GetStoreRoles(storeId),
        nameof(StoreRepository.StoreRole.Id), nameof(StoreRepository.StoreRole.Role),
        Model.Role);
    ViewData.SetLayoutModel(new LayoutModel(nameof(StoreNavPages.Users), StringLocalizer["Store Users"]).SetCategory(WellKnownCategories.Store));
}
@section PageHeadContent {
    <style>
        @@media (min-width: 576px) {
            #Role { width: auto !important; }
        }
    </style>
}
<h2 class="mb-2 mb-lg-3">@ViewData["Title"]</h2>
<partial name="_StatusMessage" />
<div class="row">
    <div class="col-xxl-constrain col-xl-8">
        <p>@ViewLocalizer["Give other registered BTCPay Server users access to your store. See the {0} for granted permissions.", Html.ActionLink(StringLocalizer["roles"], "ListRoles", "UIStores", new { storeId })]</p>

        @if (!ViewContext.ModelState.IsValid)
        {
            <div asp-validation-summary="All" class="@(ViewContext.ModelState.ErrorCount.Equals(1) ? "no-marker" : "")"></div>
        }

        <form method="post" class="d-flex flex-wrap align-items-center gap-3" permission="@Policies.CanModifyStoreSettings">
            <input asp-for="Email" type="text" class="form-control" placeholder="@StringLocalizer["user@example.com"]" style="flex: 1 1 14rem">
            <select asp-for="Role" class="form-select" asp-items="roles"></select>
            <button type="submit" role="button" class="btn btn-primary text-nowrap flex-grow-1 flex-sm-grow-0" id="AddUser" text-translate="true">Add User</button>
        </form>

        <div class="table-responsive-md">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th text-translate="true">Email</th>
                    <th text-translate="true">Name</th>
                    <th text-translate="true">Role</th>
                    <th class="actions-col" permission="@Policies.CanModifyStoreSettings"></th>
                </tr>
                </thead>
                <tbody id="StoreUsersList">
                @foreach (var user in Model.Users)
                {
                    <tr>
                        <td>@user.Email</td>
                        <td>@user.Name</td>
                        <td>@user.Role</td>
                        <td class="actions-col" permission="@Policies.CanModifyStoreSettings">
                            <div class="d-inline-flex align-items-center gap-3">
                                <a asp-action="UpdateStoreUser" asp-route-storeId="@Model.StoreId" asp-route-userId="@user.Id" data-bs-toggle="modal" data-bs-target="#EditModal" data-user-email="@user.Email" data-user-role="@user.Role" text-translate="true">Change Role</a>
                                <a asp-action="DeleteStoreUser" asp-route-storeId="@Model.StoreId" asp-route-userId="@user.Id" data-bs-toggle="modal" data-bs-target="#ConfirmModal" data-description="@StringLocalizer["This action will prevent {0} from accessing this store and its settings.", Html.Encode(user.Email)]" data-confirm-input="@StringLocalizer["Delete"]" text-translate="true">Remove</a>
                            </div>
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        </div>
    </div>
</div>

<partial name="_Confirm" model="@(new ConfirmModel(StringLocalizer["Remove store user"], StringLocalizer["This action will prevent the user from accessing this store and its settings. Are you sure?"], StringLocalizer["Delete"]))" permission="@Policies.CanModifyStoreSettings" />

<div class="modal fade" id="EditModal" tabindex="-1" aria-labelledby="EditTitle" aria-hidden="true" permission="@Policies.CanModifyStoreSettings">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="EditTitle"><span text-translate="true">Edit</span> <span id="EditUserEmail">store user</span></h4>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="@StringLocalizer["Close"]">
                    <vc:icon symbol="close" />
                </button>
            </div>
    		<form id="EditForm" method="post" rel="noreferrer noopener">
                <div class="modal-body">
                    <label asp-for="Role" for="EditUserRole" class="form-label" text-translate="true">New role</label>
                    <select asp-for="Role" id="EditUserRole" class="form-select w-auto" asp-items="roles"></select>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary only-for-js" data-bs-dismiss="modal" id="EditCancel" text-translate="true">Cancel</button>
                    <button type="submit" class="btn btn-primary" id="EditContinue" text-translate="true">Change Role</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    (function () {
        const modal = document.getElementById('EditModal')
        modal.addEventListener('show.bs.modal', event => {
            const $target = event.relatedTarget
            const $form = document.getElementById('EditForm')
            const $role = document.getElementById('EditUserRole')
            const $email = document.getElementById('EditUserEmail')
            const { userEmail, userRole } = $target.dataset
            const action = $target.dataset.action || ($target.nodeName === 'A'
                ? $target.getAttribute('href')
                : $target.form.getAttribute('action'))

            if ($form && !$form.hasAttribute('action')) $form.setAttribute('action', action)
            if (userEmail) $email.textContent = userEmail
            if (userRole) $role.value = userRole
        });
    })()
</script>

@section PageFootContent {
    <partial name="_ValidationScriptsPartial" />
}
